using Godot;
using System;
using System.Threading.Tasks;
using Legendofthebrave.Scripts.Characters;

[GlobalClass]
public partial class StateMachine: Node
{
    private StateAction _stateAction;
    private State _currentState = State.Idle;
    public float StateTime;


    public State CurrentState
    {
        get
        {
            return _currentState;
        }
        set
        {
            _stateAction.TransitionState(CurrentState, value);
            _currentState = value;
            StateTime = 0.0f;
        }
    }
    
    

    public async override void _Ready()
    {
        await ToSignal(Owner,"ready");
        _stateAction = GetParent<StateAction>();
        
    }

   
    
    public override void _PhysicsProcess(double delta)
    {
         while (true)
         {
             var next = _stateAction.GetNextState(CurrentState);
             if(CurrentState == next)
                 break;
             CurrentState = next;
         }
        
         _stateAction.TickPhysics(CurrentState, delta);
         StateTime += (float)delta;
    }
}
